//
//  __config.swift
//  eemall
//
//  Created by zhuxietong on 2018/8/27.
//  Copyright © 2018年 zhuxietong. All rights reserved.
//

import Foundation
import soapp
import MBProgressHUD
import JoLoading
// import mosdks

public class __SCconfig {
    public static var isTest = false
//    1.0 艾维 11月上线版本
//    1.1 天润商城 12月修改版本（对商户多门店结构调整，涉及app的核销门店）
    
    public static func build(){
        
        
//        _SURL.host = NetTestServer.current.host
        
//        _SURL.host = "http://test.doncheng.cn/"
        
        JoTask.defaultHandle = {
            result,url,mode in
            switch (mode ?? .normal){
            case .apijson(let apijson):
                let jsonmode = ApijsonTask(request: apijson, response: result)
                
                return jsonmode.handle
            default:
                if let dict = result as? NSDictionary{
                    let mu_dict = dict.mutable_dictionary
                    
                    if mu_dict["status",""] == "1"
                    {
                        let time = mu_dict["time",""]
                        
                        if time.jolength >= 10
                        {
                            FK.remoteTimeStamp = time
                        }
                        let data = mu_dict["data"]
                        return (.success,mu_dict["message",""],data as RootData)
                    }
                    if mu_dict["message",""].contains("token wrong"){
                        JoUser.loginUser()?.loginOut()
                        let data = mu_dict["data"]
                        return (.error,"登录已失效".loc,data as RootData)
                    }
                    if mu_dict["status",""] == "3"
                    {
                        JoUser.loginUser()?.loginOut()
                        let data = mu_dict["data"]
                        return (.error,"登录已失效".loc,data as RootData)
                    }
                    if mu_dict["status",""] == "0"
                    {
                        //                        let msg = mu_dict["message",""]
                        //                        if msg.contains("token wrong"){
                        //                            JoUser.loginUser()?.loginOut()
                        //                        }
                        let data = mu_dict["data"]
                        return (.error,mu_dict["message",""],data as RootData)
                    }
                }
                return (.error,"",NSMutableDictionary())
                
            }
            
            
            
            
        }
        
 
        //
        //        JoUser.unBindPushTokenBlock = {
        //            SoNum.clear()
        //
        //            CloudPushSDK.unbindAccount { (result) in
        //                if let r = result{
        //                    if r.success{
        //                        debugPrint("unbind CloudPushSDK success")
        //                    }
        //                    else{
        //                        debugPrint("unbind CloudPushSDK error :\(String(describing: r.error))")
        //                    }
        //                }
        //            }
        //        }
        //
        
//        AppLink.routers.append(AppLinkType.self)//添加点击路由
//        EeMsgType.appendObserver(types: AppMsgType.types)//添加红的标记类型
        Date.stamp精度 = .秒//设置项目时间戳精确度
//        EeTopStyle.default = __theme.white() //设置默认的top_style  与NavigationBar,statusBar 样式有关
        
        //全局设置每个 http 头部的参数，本项目中的http 是token头部授权
        //        JoTask.headerCreator = {
        //            let headers = [String:String]()
        //            return headers
        //        }
        //
        JoTask.headerCreator = { _ in
            var ps = [String:String]()
            if let token = JoUser.loginUser()?.token
            {
                ps["token"] = token
                
            }
            ps["device-type"] = "iOS"
            ps["device-id"] = __app.device_id
            ps["x-timestamp"] = Date()["STAMP"]
            ps["client-version"] = __app.client_version
            ps["system-version"] = __app.system_verion
            
            return ps
        }
        
        
        JoTask.debugLog = true//开启全局JoTask 网络试调打印
        WebController.tinyColor = UIColor(shex:AppTheme.main_hex) //设置WebController 价值进度条的颜色
        
        
        PageConfig.page_key = "page"
        PageConfig.page_size_key = "pagesize"
        PageConfig.page_begin = 1
        PageConfig.is_index_page_key = false
        //        PageConfig.dataStatusJudge = __dataStatusJudge
        
        LoadingPresenterDefautLoadingClass = JoDefaultLoading.self//全局设置LoadingPresenter 加载控件
 
//        
        __images =
        [
                "http://a.hiphotos.baidu.com/image/pic/item/30adcbef76094b36d4bfddfea9cc7cd98d109df0.jpg",
        ]
        
        CoHud._loading = { view in
            
            if let a_view = view{
                
                if let hud = a_view.viewWithTag(98989) as? MBProgressHUD{
                    hud.backgroundView.style = .solidColor
                    hud.backgroundView.color = UIColor(shex: "#00000016")
                    hud.tag = 98989
                    return
                }
                
                let hud = MBProgressHUD.showAdded(to: a_view, animated: true)
                hud.backgroundView.style = .solidColor
                hud.backgroundView.color = UIColor(shex: "#00000016")
                hud.tag = 98989
                a_view.isUserInteractionEnabled = false
            }
        }
        CoHud._dismiss = { view in
            if let a_view = view{
                MBProgressHUD.hide(for: a_view, animated: true)
                a_view.isUserInteractionEnabled = true
            }
        }
        
    }
    
}
